package com.dp.service.jdc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.News;
import com.dp.pojo.NewsVo;
import com.dp.pojo.PageCtrl;
import com.dp.service.impl3.BaseImpl;
import com.dp.vo.TChannels;
import com.dp.vo.TRoles;

@Service
@Transactional
public class ChannelServiceImpl3 extends BaseImpl {

    @SuppressWarnings("unchecked")
    public PageCtrl getPageCtrl(String channelId,int pageNo,int pageSize){
        PageCtrl pageCtrl = new PageCtrl();
        String sql = "select count(*) from news_extends_channel nec ,news_extends_channel_title nect, news_contenttitle nc"+
        " where  nect.contentstatus = 1 and nc.cid =  nect.cid and nec.channel_id ='"+channelId+"' and  nect.channleid = nec.channleid ";
        try{
            int resultCount = Integer.valueOf( this.em.createNativeQuery(sql).getSingleResult().toString());
            pageCtrl.setResultCount(resultCount);
            pageCtrl.setPageNo(pageNo);
            pageCtrl.setPageSize(pageSize);
            pageCtrl.setPageCount(resultCount%pageSize==0?resultCount/pageSize:resultCount/pageSize+1);
            return pageCtrl;
            
        }catch (Exception e) {
            logger.error(" ChannelServiceImpl3-->>getPageCtrl fail !!!" +e.getMessage());
            e.printStackTrace();
            return null;
        }
        
        
    }
    /**根据channelId获取同一级别的所有栏目
     * 左侧菜单栏栏目
     * @param channelId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<TChannels> getChannels(String channelId){
        List<TChannels> channelList = null; 
        this.sql = "SELECT t FROM TChannels t where t.previousId=" +
        		"(SELECT t2.previousId FROM TChannels t2 where t2.channelId='"+channelId+"')";
        try {
            channelList=this.getQueryList(sql);
            return channelList;
            
        } catch (Exception e) {
            logger.error("ChannelServiceImpl3-->> getChannels fail !!!" +e.getMessage());
            e.printStackTrace();
            return null;
        }
        
    }
    /**以前根据channelId获取下一级的所有栏目
     * 左侧菜单栏栏目
     * @param channelId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<TChannels> getLastChannels(String channelId){
        List<TChannels> channelList = null; 
        this.sql = "SELECT t FROM TChannels t where t.previousId='"+channelId+"' order by t.channelId";
        try {
            channelList=this.getQueryList(sql);
            return channelList;
            
        } catch (Exception e) {
            logger.error("ChannelServiceImpl3-->> getChannels fail !!!" +e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
    /**
     * @param channelId:栏目名称
     * @return channelNames上几级栏目名称列表
     */
    @Deprecated
    @SuppressWarnings("unchecked")
    public List<TChannels> getTopChannel(String channelId){
        List<TChannels> channelNames = new ArrayList<TChannels>();
        TChannels tc = null; 
        do {
            tc = this.getChannelById(channelId);
            if(tc==null){
                break;
            }else {
                
                channelNames.add(tc);
                channelId = tc.getPreviousId();
            }
        } while (!tc.getLevel().equals("3"));
        try{
            Collections.reverse(channelNames);
        }catch (Exception e) {
            logger.error(" getChannelNames目录名称倒序失败!!! fail !!!" +e.getMessage());
            e.printStackTrace();
        }

          return  channelNames;
    }
    
    
    
    /**根据栏目id获取栏目
     * @param channelId栏目名称
     * @return 栏目内容
     */
    @SuppressWarnings("unchecked")
    public TChannels getChannelById(String channelId) {
        TChannels tc ;
        String hql = "SELECT t FROM TChannels t WHERE t.channelId ='"+channelId+"'";
        try {
            tc = new TChannels();
            tc = (TChannels) this.getSingleResult(hql);
            return tc;
            
        } catch (Exception e) {
            logger.error(" getChannelById" +e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
    
    
    
    
    
	/**根据栏目的ＩＤ获取栏目下新闻内容的标题
	 * @param channelId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<News> getNews(String channelId,int pageSize,int pageNo) {
		List<News> newList = new ArrayList<News>(); 
		int resultLimtBegin =  pageSize*pageNo;
		this.sql = "select nc.contenttitle,nec.channel_id,nc.cid,nc.updatetime,nc.contentid,nc.contenticon from news_extends_channel nec ,news_extends_channel_title nect, news_contenttitle nc"+
		        " where  nc.cid =  nect.cid and nec.channel_id ='"+channelId+"' and nect.contentstatus = 1 and nect.channleid = nec.channleid order by nect.titleorder,nect.cid desc limit "+resultLimtBegin+","+pageSize;
		News news = null;
		try {
		    List<Object[]> temp=this.em.createNativeQuery(sql).getResultList();
			for(Object[]o:temp)
            {
                news = new News();
                news.setContentName(this.getString(o[0]));
                news.setChannelId(this.getString(o[1]));
                news.setCId(this.getString(o[2]));
                news.setCreatedDate(this.getString(o[3]));
                news.setContentId(this.getString(o[4]));
                news.setContentIcon(this.getString(o[5]));
                newList.add(news);
            }
		} catch (Exception e) {
			logger.error(" getNewsChannelId fail !!!" +e.getMessage());
			e.printStackTrace();
		}
		return newList;
	}
	
	
}